package cn.sciento.starter.tenant.custom.core;

import cn.sciento.core.oauth.CustomUserDetails;
import cn.sciento.core.oauth.DetailsHelper;
import org.apache.commons.lang3.StringUtils;

public class TenantCustomContext {
  public static final String FIELD_TENANT_NUM = "tenantNum";
  
  private static final ThreadLocal<String> TENANT_NUM = new ThreadLocal<>();
  
  public static String getTenantNum() {
    String tenantNum = TENANT_NUM.get();
    if (StringUtils.isBlank(tenantNum)) {
      CustomUserDetails details = DetailsHelper.getUserDetails();
      if (details != null)
        tenantNum = details.getTenantNum(); 
    } 
    return tenantNum;
  }
  
  public static void setTenantNum(String tenantNum) {
    TENANT_NUM.set(tenantNum);
  }
  
  public static void clear() {
    TENANT_NUM.remove();
  }
}
